<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_produits extends CI_Controller {
	
	function index()
	{
		$data['error'] ='display: none;';
		$data['produit']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{	
				$data['title'] = 'Admin produits';
				$this->load->model('admin_model');	
				$data['produits'] = $this->home_model->list_produits();	
				$data['message'] = $this->admin_model->list_message();
				$data['content'] = 'admin/admin_produits';
				$data['produit']  ='active';
				$this->load->view('admin/template/template',$data);
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}

	function delete_produits()
	{
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
			if($this->uri->segment(3))
			{
				
			$this->load->model('admin_model');
			$data['row'] = $this->admin_model->get_produits($this->uri->segment(3));
			$IMAGE = $data['row']->photos;
			
			@unlink('uploads/produits/'.$IMAGE);
			@unlink('uploads/produits/thumbs/'.$IMAGE);	
			
			$this->load->model('crud_model');
			$this->crud_model->delete_produits($this->uri->segment(3));
			
			$data['title'] = 'Admin produits';	
			$data['produits'] = $this->home_model->list_produits();
			$data['message'] = $this->admin_model->list_message();	
			$data['content'] = 'admin/admin_produits';
			$data['success'] = 'display:block;';
			$this->load->view('admin/template/template',$data);
			}
			else 
			{
			
			$data['error'] = 'display: block;';
			$data['title'] = 'Admin produits';
			$this->load->model('admin_model');
			$this->load->model('home_model');
			$data['produits'] = $this->home_model->list_produits();	
			$data['message'] = $this->admin_model->list_message();	
			$data['content'] = 'admin/admin_produits';
			$this->load->view('admin/template/template',$data);
			
			}
		}
		else {
			$this->load->view('admin/signup');
		}
	}

	function update_produits()
	{
		$data['error'] ='display: none;';
		$data['produit']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
				
			$data['title'] = 'Editer produits';
			$this->load->model('admin_model');
			if ($this->uri->segment(3)) 
			{
			$this->load->model('admin_model');

			$data['rows'] = $this->admin_model->get_produits($this->uri->segment(3));
			// variable transmis à la vue
			$data['id'] = $data['rows']->id_produits;
			$data['title_fr'] = $data['rows']->title_fr;
			$data['description_fr'] = $data['rows']->description_fr;
			$data['title_en'] = $data['rows']->title_en;
			$data['description_en'] = $data['rows']->description_en;
			$data['photos'] = $data['rows']->photos;

			$data['title'] = 'Editer produits';
			$this->load->model('admin_model');	
			$data['message'] = $this->admin_model->list_message();

			} 
			else 
			{
				redirect('admin_produits');
			}
			
			
			$data['content'] = 'admin/admin_produits_edit';
			$this->load->view('admin/template/template',$data);	
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}




	function update_upload(){
		if($this->session->userdata('email') || $this->session->userdata('logged')) 
		{
			if($this->uri->segment(3))
			{
				$this->load->model('admin_model');
				$data['message'] = $this->admin_model->list_message();
				$data['rows'] = $this->admin_model->get_produits($this->uri->segment(3));
				// variable transmis à la vue

				$data['id'] = $data['rows']->id_produits;
				$data['title_fr'] = $data['rows']->title_fr;
				$data['description_fr'] = $data['rows']->description_fr;

				$data['title_en'] = $data['rows']->title_en;
				$data['description_en'] = $data['rows']->description_en;

				$data['photos'] = $data['rows']->photos;
				
				//condition upload
				$config['upload_path'] = './uploads/produits/';
				$config['allowed_types'] = 'gif|png|jpeg|jpg';
				$config['max_size'] = '3048';
				$config['encrypt_name'] = true;

				$this->load->library('upload',$config);

				if (!$this->upload->do_upload()) 
				{
					$this->lang->load('upload', $this->config->item('language'));
					$data['error_upload'] = $this->upload->display_errors();
					$data['title'] = 'Editer produits';
					$data['content'] = 'admin/admin_produits_edit';
					$this->load->view('admin/template/template',$data);
				}
				else 
				{
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					//-- supprimer image ancienne
					$donne['row'] = $this->admin_model->get_produits($this->uri->segment(3));
					echo $IMAGE = $donne['row']->photos;
					@unlink('uploads/produits/'.$IMAGE);
					@unlink('uploads/produits/thumbs/'.$IMAGE);
					//update 
					$image_data = $this->upload->data();
					$config = array(
						'image_library' => 'GD2', 
						'source_image' => $image_data['full_path'],
						'new_image' => './uploads/produits/thumbs/',
						'create_thumb' => TRUE,
						'thumb_marker' => '',
						'maintain_ratio' => FALSE,
						'width'=> 185,
						'height' => 220
					);
					$this->load->library('image_lib',$config);
					$this->image_lib->resize();
					$image_name = $image_data['file_name'];
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data = array(
						'photos' => $image_name,
						'title_fr' => $this->input->post('title_fr'),
						'description_fr' => $this->input->post('comment_fr'),
						'title_en' => $this->input->post('title_en'),
						'description_en' => $this->input->post('comment_en')
					);
					$this->admin_model->update_produits($this->uri->segment(3),$data);

					$data['success'] ='display: block;';
					$this->load->model('admin_model');	
					$data['produits'] = $this->home_model->list_produits();	
					$data['message'] = $this->admin_model->list_message();
					$data['content'] = 'admin/admin_produits';
					$this->load->view('admin/template/template',$data);
					
				}
				
			}
			else 
			{
					$data['error'] ='display: block;';
					$this->load->model('admin_model');	
					$data['produits'] = $this->home_model->list_produits();	
					$data['message'] = $this->admin_model->list_message();
					$data['content'] = 'admin/admin_produits';
					$this->load->view('admin/template/template',$data);	
			}		
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
	
	
	
}		
?>